<?php
session_start();
if(!isset($_SESSION['uid']))
 	{?>
		<script type="text/javascript">
			window.location="error.php";
		</script>
	<?php 
 	}else 		
 		{
			require_once 'dataBaseConnectivityFile.php';
			$time_stamp=date('d_M_Y_H.i.s');
			$fileName = $_FILES["image"]["name"];
			$fileTmpLoc = $_FILES["image"]["tmp_name"];
			$pathAndName = "CarImages/".$time_stamp."_".$fileName;
			$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
			
			$TITLE=$_REQUEST["title"];			
			$CONDITION=$_REQUEST["condition"];
			$MAKE=$_REQUEST["make"];
			$BODY_STYLE=$_REQUEST["body_style"];
			$START_YEAR=$_REQUEST["start_year"];
			$END_YEAR=$_REQUEST["end_year"];
			$MILEAGE=$_REQUEST["mileage"];
			$TRANSMISSION=$_REQUEST["transmission"];
			$DRIVE_TRAIN=$_REQUEST["drivetrain"];
			$EXTERIOR_COLOR=$_REQUEST["exterior_color"];
			$INTERIOR_COLOR=$_REQUEST["interior_color"];
			$FUEL_TYPE=$_REQUEST["fuel_type"];
			$CITY_MILES_PER_GALLON=$_REQUEST["city_miles"];
			$HIGHWAY_MILES_PER_GALLON=$_REQUEST["highway_miles"];
			$CAR_STATUS=$_REQUEST["select"];
			$PRICE=$_REQUEST["PRICE"];
			$CONTACT=$_REQUEST["CONTACT"];
			$IMAGE=$_REQUEST["image"];
			$MODEL_NUMBER=$_REQUEST["model_number"];
			$ENGINE=$_REQUEST["engine"];
			$VIN=$_REQUEST["vin"];
						
		if($fileName=="")
		{
					       $sql_query="INSERT INTO stock (TITLE,`CONDITION`, MAKE, BODY_STYLE, START_YEAR, END_YEAR, MILEAGE, TRANSMISSION, 
					       				DRIVE_TRAIN, EXTERIOR_COLOR, INTERIOR_COLOR, FUEL_TYPE, CITY_MILES_PER_GALLON, HIGHWAY_MILES_PER_GALLON,
			       						`STATUS`, PRICE ,CONTACT_NO, IMAGE_PATH, CREATE_DATE, MODIFIED_DATE, `ENGINE`, MODEL_NUMBER, VIN)
                                 VALUES('".$TITLE."','".$CONDITION."', '".$MAKE."', '".$BODY_STYLE."', '".$START_YEAR."', '".$END_YEAR."',".
                                 "'".$MILEAGE."', '".$TRANSMISSION."', '".$DRIVE_TRAIN."', '".$EXTERIOR_COLOR."', '".$INTERIOR_COLOR."',". 
                                 "'".$FUEL_TYPE."', '".$CITY_MILES_PER_GALLON."', '".$HIGHWAY_MILES_PER_GALLON."',".
                                 "'".$CAR_STATUS."', '".$PRICE."'	,'".$CONTACT."', '', NOW(), NOW(),	'".$ENGINE."',	'".$MODEL_NUMBER."',	'".$VIN."' )";
					       
							       if(!mysql_query($sql_query,$con))
							                {
							                   die ("ERROR ".mysql_error());
							                }else
							                {?>
						                        <script type="text/javascript">
						                            alert("Car Information added");
						                            window.location="viewCar.php";
						                        </script>							                	
							               <?php  }	?>
<?php		}else
		 {	
			if($moveResult==true)
			    {   	    	
			       $sql_query="INSERT INTO stock (TITLE,`CONDITION`, MAKE, BODY_STYLE, START_YEAR, END_YEAR, MILEAGE, TRANSMISSION, DRIVE_TRAIN, EXTERIOR_COLOR, INTERIOR_COLOR, FUEL_TYPE, CITY_MILES_PER_GALLON, HIGHWAY_MILES_PER_GALLON,
			       				`STATUS`, PRICE ,CONTACT_NO, IMAGE_PATH, CREATE_DATE, MODIFIED_DATE, `ENGINE`, MODEL_NUMBER, VIN)
                                 VALUES('".$TITLE."','".$CONDITION."', '".$MAKE."', '".$BODY_STYLE."', '".$START_YEAR."', '".$END_YEAR."',".
                                 "'".$MILEAGE."', '".$TRANSMISSION."', '".$DRIVE_TRAIN."', '".$EXTERIOR_COLOR."', '".$INTERIOR_COLOR."',". 
                                 "'".$FUEL_TYPE."', '".$CITY_MILES_PER_GALLON."', '".$HIGHWAY_MILES_PER_GALLON."',".
                                 "'".$CAR_STATUS."', '".$PRICE."', '".$CONTACT."', '".$pathAndName."', NOW(), NOW(),  '".$ENGINE."', '".$MODEL_NUMBER."', '".$VIN."' )";
			       
			       if(!mysql_query($sql_query,$con))
			                {
			                   die ("ERROR ".mysql_error());
			                }else
			                    {?>
			                        <script type="text/javascript">
			                            alert("Car Information added");
			                            window.location="viewCar.php";
			                        </script>
			
			                    <?php
			                    }
			    }
		else
			                    {?>
			                        <script type="text/javascript">
			                            alert("Error adding the Car Information")
			                            history.back();
			                        </script>
			                    <?php
			                    }
			 		}
          }?>		